﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace SharePointAccelerators.ApplyLanguage.ApplyLanguageEventReceiver
{
    /// <summary>
    /// Web Events
    /// </summary>
    public class ApplyLanguageEventReceiver : SPWebEventReceiver
    {
       /// <summary>
       /// A site was provisioned.
       /// </summary>
       public override void WebProvisioned(SPWebEventProperties properties)
       {
           // will be applied if RootWeb of SiteCollection is also Multilingual
           if (properties.Web.Site.RootWeb.IsMultilingual)
           {
               SPWeb web = properties.Web;
               web.IsMultilingual = true;

                SPLanguageCollection installed = SPRegionalSettings.GlobalInstalledLanguages;
                IEnumerable<CultureInfo> supported = web.SupportedUICultures;

                foreach (SPLanguage language in installed)
                {
                    CultureInfo culture = new CultureInfo(language.LCID);

                    if (!supported.Contains(culture))
                    {
                        //Console.WriteLine("Adding {0}", culture.Name);
                        web.AddSupportedUICulture(culture);
                    }
                }
                web.Update();
           }

           base.WebProvisioned(properties);
       }


    }
}
